package org.jeecg.modules.flowable.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.flowable.dto.FlowInstanceStartDTO;
import org.jeecg.modules.flowable.dto.FlowInstanceStopDTO;
import org.jeecg.modules.flowable.entity.FlowInstance;
import org.jeecg.modules.flowable.entity.FlowModel;
import org.jeecg.modules.flowable.entity.FlowOperationInfo;
import org.jeecg.modules.flowable.service.FlowInstanceService;
import org.jeecg.modules.flowable.service.FlowModelService;
import org.jeecg.modules.flowable.service.FlowOperationInfoService;
import org.jeecg.modules.flowable.vo.FlowInstanceVO;
import org.jeecg.modules.flowable.vo.FlowModelVO;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>工作流流程实例管理<p>
 */
@Slf4j
@Api(tags = "工作流流程实例管理")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flow/instance")
public class FlowInstanceController {

    final FlowInstanceService instanceService;

    final FlowModelService flowModelService;

    final FlowOperationInfoService flowOperationInfoService;

    @ApiOperation(value = "根据流程定义id启动流程实例")
    @PostMapping("/start")
    public Result<Void> startProcessInstance(@RequestBody FlowInstanceStartDTO operationDTO) {
        return this.instanceService.start(operationDTO);
    }

    @ApiOperation("结束流程实例")
    @PostMapping(value = "/stop")
    public Result<Void> stopProcessInstance(@RequestBody FlowInstanceStopDTO operationDTO) {
        return this.instanceService.stop(operationDTO);
    }

    @ApiOperation(value = "流程操作记录")
    @GetMapping(value = "/operation/list")
    public Result<Void> operationList(String dataId) {
        return Result.OK();
    }


    @ApiOperation(value = "获取当前实例信息")
    @GetMapping(value = "/info/{instanceId}")
    public Result<FlowInstanceVO> info(@PathVariable String instanceId) {

        return this.instanceService.info(instanceId);
    }

    @ApiOperation(value = "获取当前实例信息")
    @GetMapping(value = "/information/{instanceId}")
    public Result<FlowModelVO> information(@PathVariable String instanceId) {
        FlowInstance flowInstance = this.instanceService.getById(instanceId);
        FlowModel flowModel = flowModelService.searchByBussinessKey(flowInstance.getBusinessKey());
        FlowModelVO flowModelVO = flowModelService.loadBpmnXmlByModelId(flowModel.getId()).getResult();
        List<FlowOperationInfo> flowOperationInfoList = flowOperationInfoService.searchsByInstanceId(instanceId);
        if(oConvertUtils.isNotEmpty(flowOperationInfoList))
            flowModelVO.setFlowOperationInfoList(flowOperationInfoList);
        return Result.OK(flowModelVO);
    }
}
